﻿using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using Newtonsoft.Json.Serialization;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace GN.Pay.Utils
{
    /// <summary>
    /// 公共扩展
    /// </summary>
    public static class CommonExtension
    {
        private static IsoDateTimeConverter timeFormat = new IsoDateTimeConverter() { DateTimeFormat = "yyyy-MM-dd HH:mm:ss" };

        private static JsonSerializerSettings CamelCase_Setting = new JsonSerializerSettings()
        {
            ContractResolver = new CamelCasePropertyNamesContractResolver(),
            DateFormatHandling = DateFormatHandling.IsoDateFormat,
            DateFormatString = "yyyy-MM-dd HH:mm:ss"

        };

        private static JsonSerializerSettings Default_Setting = new JsonSerializerSettings()
        {
            ContractResolver = new DefaultContractResolver(),
            DateFormatHandling = DateFormatHandling.IsoDateFormat,
            DateFormatString = "yyyy-MM-dd HH:mm:ss"
        };


        /// <summary>
        /// 转换为Json对象
        /// </summary>
        /// <param name="value">值</param>
        /// <param name="isCamelCase">是否转换为骆锋命名</param>
        /// <returns></returns>
        public static string ToJson(this object value, bool isCamelCase)
        {
            if (value == null)
            {
                return null;
            }
            if (isCamelCase)
            {
                return JsonConvert.SerializeObject(value, CamelCase_Setting);
            }
            return JsonConvert.SerializeObject(value, Default_Setting);
        }

        /// <summary>
        /// Json转换为对象
        /// </summary>
        /// <param name="value"></param>
        /// <param name="type">类型</param>
        /// <param name="isCamelCase"></param>
        /// <returns></returns>
        public static object JsonToObject(this string value, Type type, bool isCamelCase)
        {
            if (string.IsNullOrWhiteSpace(value))
            {
                return null;
            }
            if (isCamelCase)
            {
                return JsonConvert.DeserializeObject(value, type, CamelCase_Setting);
            }
            return JsonConvert.DeserializeObject(value, type, Default_Setting);
        }

        /// <summary>
        /// Json转换为对象
        /// </summary>
        /// <param name="value"></param>
        /// <param name="isCamelCase"></param>
        /// <returns></returns>
        public static T JsonToObject<T>(this string value, bool isCamelCase)
        {
            if (string.IsNullOrWhiteSpace(value))
            {
                return default(T);
            }
            if (isCamelCase)
            {
                return JsonConvert.DeserializeObject<T>(value, CamelCase_Setting);
            }
            return JsonConvert.DeserializeObject<T>(value, Default_Setting);
        }

        public static string ReadStreamString(this Stream stream)
        {
            if (stream == null)
            {
                return "";
            }
            //使用读取流读取响应结果
            using (StreamReader reader = new StreamReader(stream))
            {
                return reader.ReadToEnd();
            }
        }
    }
}
